//============================================================================
// Name        : main.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include "Common.h"
#include "UdpServer.h"
#include "Pl.h"
#include "ShareDDR.h"

using namespace std;

CUdpServer  udp_server;

void init_app()
{

	g_IsRunning = true;

	g_pl_axilite = new  Pl();
	g_pl_axilite->AXI_Lite_Init();

	g_share_mem = new CShareMem();
	g_share_mem->CShareMem_Init();

	udp_server.start();

	cout << "working ......" << endl;
}

void close_app(int s)
{
	g_IsRunning = 0;
	sleep(1);
	udp_server.Close();
	g_pl_axilite->AXI_Lite_Close();
	sleep(1);
	cout << " Stop !!!" << endl;
	exit(0);
}


int main()
{
	init_app();
	signal(SIGINT, close_app);
	unsigned int version_value;


	unsigned int value1;
	unsigned int value1_old;


	unsigned int value2;
	unsigned int value2_old;


	unsigned int value3;
	unsigned int value3_old;


	unsigned int value4;
	unsigned int value4_old;


	unsigned int value5;
	unsigned int value5_old;



	unsigned int value6;
	unsigned int value6_old;


	unsigned int value7;
	unsigned int value7_old;


	unsigned int value8;
	unsigned int value8_old;

	g_pl_axilite->AXI_Lite_Read(VERSION,version_value);
	printf("version = %x\r\n",version_value);
	sleep(2);

	FILE *file1;
	char filename1[] = "output1.bin";
	file1 = fopen(filename1, "wb");

	FILE *file2;
	char filename2[] = "output2.bin";
	file2 = fopen(filename2, "wb");

	FILE *file3;
	char filename3[] = "output3.bin";
	file3 = fopen(filename3, "wb");

	FILE *file4;
	char filename4[] = "output4.bin";
	file4 = fopen(filename4, "wb");

	FILE *file5;
	char filename5[] = "output5.bin";
	file5 = fopen(filename5, "wb");

	FILE *file6;
	char filename6[] = "output6.bin";
	file6 = fopen(filename6, "wb");

	FILE *file7;
	char filename7[] = "output7.bin";
	file7 = fopen(filename7, "wb");

	FILE *file8;
	char filename8[] = "output8.bin";
	file8 = fopen(filename8, "wb");
	unsigned int buf[1024];



	while(1)
	{
		g_pl_axilite->AXI_Lite_Read(CHANNEL_AD1,value1);
		if(value1_old != value1)
		{
			value1_old = value1;
			//printf("CHANNEL_AD1 = %x\r\n",value1);
			g_share_mem->CShareMem_Read2(0,256,buf);
		    size_t written = fwrite(buf, sizeof(unsigned int), 256, file1);
		    fflush(file1);

		    //fclose(file);
		}

		g_pl_axilite->AXI_Lite_Read(CHANNEL_AD2,value2);
		if(value2_old != value2)
		{
			value2_old = value2;
			//printf("CHANNEL_AD2 = %x\r\n",value2);
			g_share_mem->CShareMem_Read2((0x4000),256,buf);
		    size_t written = fwrite(buf, sizeof(unsigned int), 256, file2);
		    fflush(file2);


		}

		g_pl_axilite->AXI_Lite_Read(CHANNEL_AD3,value3);
		if(value3_old != value3)
		{
			value3_old = value3;
			//printf("CHANNEL_AD3 = %x\r\n",value3);
			g_share_mem->CShareMem_Read2((0x4000*2),256,buf);
		    size_t written = fwrite(buf, sizeof(unsigned int), 256, file3);
		    fflush(file3);
		}

		g_pl_axilite->AXI_Lite_Read(CHANNEL_AD4,value4);
		if(value4_old != value4)
		{
			value4_old = value4;
			//printf("CHANNEL_AD4 = %x\r\n",value4);
			g_share_mem->CShareMem_Read2((0x4000*3),256,buf);
		    size_t written = fwrite(buf, sizeof(unsigned int), 256, file4);
		    fflush(file4);
		}

		g_pl_axilite->AXI_Lite_Read(CHANNEL_AD5,value5);
		if(value5_old != value5)
		{
			value5_old = value5;
			//printf("CHANNEL_AD5 = %x\r\n",value5);
			g_share_mem->CShareMem_Read2((0x4000*4),256,buf);
		    size_t written = fwrite(buf, sizeof(unsigned int), 256, file5);
		    fflush(file5);
		}

		g_pl_axilite->AXI_Lite_Read(CHANNEL_AD6,value6);
		if(value6_old != value6)
		{
			value6_old = value6;
			//printf("CHANNEL_AD6 = %x\r\n",value6);
			g_share_mem->CShareMem_Read2((0x4000*5),256,buf);
		    size_t written = fwrite(buf, sizeof(unsigned int), 256, file6);
		    fflush(file6);
		}


		g_pl_axilite->AXI_Lite_Read(CHANNEL_AD7,value7);
		if(value7_old != value7)
		{
			value7_old = value7;
			//printf("CHANNEL_AD7 = %x\r\n",value7);
			g_share_mem->CShareMem_Read2((0x4000*6),256,buf);
		    size_t written = fwrite(buf, sizeof(unsigned int), 256, file7);
		    fflush(file7);
		}

		g_pl_axilite->AXI_Lite_Read(CHANNEL_AD8,value8);
		if(value8_old != value8)
		{
			value8_old = value8;
			//printf("CHANNEL_AD8 = %x\r\n",value8);
			g_share_mem->CShareMem_Read2((0x4000*6),256,buf);
		    size_t written = fwrite(buf, sizeof(unsigned int), 256, file8);
		    fflush(file8);
		}

		usleep(200);
	}

	udp_server.join();

	return 0;
}
